package com.example.as_sum.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.as_sum.R;
import com.example.as_sum.entity.Student;
import com.example.as_sum.util.DataBaseUtil;
import com.litesuits.orm.db.assit.WhereBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DataBaseActivity extends AppCompatActivity {

    @Bind(R.id.data_ed)
    EditText mDataEd;
    @Bind(R.id.data_tv)
    TextView mDataTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_base);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.data_insert1, R.id.data_delete, R.id.data_delete2, R.id.data_update1,
            R.id.data_quary1,R.id.btn_daba_update,R.id.btn_daba_insert,R.id.btn_daba_quary})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.data_insert1:
                List<Student> list=new ArrayList<>();
                for(int x=0;x<5;x++){
                    Student student=new Student(100+x,"李发染"+x,"真帅啊"+x);
                    list.add(student);
                }
                DataBaseUtil.insertAll(list);
                showCon(list);
                break;
            case R.id.data_delete:
                List<Student> students=DataBaseUtil.getQueryAll(Student.class);
                showCon(students);
                break;
            case R.id.data_delete2:
                DataBaseUtil.delete(Student.class);
                break;
            case R.id.data_update1:
                String ids=mDataEd.getText().toString();
                if(!TextUtils.isEmpty(ids)) {
                    HashMap<String,Object> map=new HashMap<>();
                    map.put("name","高圆圆");
                    int type=DataBaseUtil.updateData(Student.class, "sid",ids,map);
                    if(type==0){
                        DataBaseUtil.insert(new Student(Long.valueOf(ids),"李先生","李先生很帅"));
                    }
                }else{
                    showMsg("id不能为空");
                }
                break;
            case R.id.data_quary1:
                String id=mDataEd.getText().toString();
                if(!TextUtils.isEmpty(id)) {
                    //查询
                    List<Student> st=DataBaseUtil.getQueryByWhere(Student.class,"sid",id);
                    showCon(st);
                } else{
                    showMsg("id不能为空");
                }
                break;
            case R.id.btn_daba_update:
                break;
            case R.id.btn_daba_insert:
                List<Student> students1=new ArrayList<>();
                for(int x=0;x<5;x++){
                    Student student=new Student(100+x,"李发染"+x,"真帅啊"+x,"男","101");
                    students1.add(student);
                }
                DataBaseUtil.insertAll(students1);
                break;
            case R.id.btn_daba_quary:
                List<Student> studentaa=DataBaseUtil.getQueryAll(Student.class);
                showCon(studentaa);
                break;
        }
    }
    private void showMsg(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }
    private void showCon(Object o){
        if(o instanceof Student) {
            Student student =(Student) o;
            String msg="id："+student.getId()+","+student.getName()+","+student.getInfo();
            mDataTv.setText(msg);
        }else if(o instanceof List){
            List<Student> list=(List<Student>)o;
            StringBuffer msg=new StringBuffer();
            for (Student student:list){
                msg.append("id："+student.getId()+","+student.getName()+","+student.getInfo()+"\n");
            }
            mDataTv.setText(msg.toString());
        }else{
            mDataTv.setText("无结果");
        }
    }
}
